﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class AddQuestion : BasePage
{
    SqlDataAdapter daTable = null;
    DataTable dtTable = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        HttpCookie loginUser = Request.Cookies.Get("LoginUser");

        if (Session["userType"] == null || Session["userType"].ToString() != "PhongDaoTao")
        {
            Response.Redirect("~/Default.aspx");
        }
        else
        {
            LoadData();
        }
    }
    protected void btnLuu_Click(object sender, EventArgs e)
    {
        if (RadioButtonThem.SelectedValue == "Thêm Nhóm Tiêu Chí")
        {
            try
            {
                SqlConnection conn = new SqlConnection(connectionString);
                conn.Open();
                SqlCommand cmd = new SqlCommand("Insert into NhomTieuChi(MaNTC,TenNTC) Values('" + txtMa.Text + "',N'" + txtTen.Text + "')", conn);
                cmd.ExecuteNonQuery();
                conn.Close();
                ShowMessage("Thêm Nhóm tiêu chí thành công!", "");
            }
            catch(SqlException)
            {
                ShowMessage("Thêm nhóm tiêu chí thất bại !", "");
            }
        }
        if (RadioButtonThem.SelectedValue == "Thêm Tiêu Chí")
        {
            try
            {
                string MaNTC = DropDownListMaNTC.SelectedValue.ToString();
                SqlConnection conn = new SqlConnection(connectionString);
                conn.Open();
                SqlCommand cmd = new SqlCommand("Insert into TieuChi(MaTC,MaNTC,TenTC) Values('" + txtMa.Text + "','" + MaNTC + "',N'" + txtTen.Text + "')", conn);
                cmd.ExecuteNonQuery();
                conn.Close();
                ShowMessage("Thêm tiêu chí thành công!", "");
            }
            catch (SqlException)
            {
                ShowMessage("Thêm tiêu chí thất bại !", "");
            }
        }
    }
    protected void LoadData()
    {
        DropDownListMaNTC.Visible = false;
        lblMa.Visible = false;
        lblTen.Visible = false;
        txtTen.Visible = false;
        txtMa.Visible = false;
        btnLuu.Visible = false;
        lblNTC.Visible = false;
        if(RadioButtonThem.SelectedValue == "Thêm Nhóm Tiêu Chí")
        {
            DropDownListMaNTC.Visible = false;
            lblMa.Text = "Mã Nhóm Tiêu Chí:";
            lblMa.Visible = true;
            lblTen.Text = "Tên Nhóm Tiêu Chí:";
            lblNTC.Visible = false;
            lblTen.Visible = true;
            txtTen.Visible = true;
            txtMa.Visible = true;
            btnLuu.Visible = true;
        }
        if (RadioButtonThem.SelectedValue == "Thêm Tiêu Chí")
        {

            SqlConnection conn = new SqlConnection(connectionString);
            conn.Open();
            daTable = new SqlDataAdapter("select * from NhomTieuChi", conn);
            dtTable = new DataTable();
            dtTable.Clear();
            daTable.Fill(dtTable);

            DropDownListMaNTC.Items.Clear();
            DropDownListMaNTC.DataSource = dtTable;
            DropDownListMaNTC.DataTextField = "TenNTC";
            DropDownListMaNTC.DataValueField ="MaNTC";
            DropDownListMaNTC.DataBind();
            conn.Close();

            DropDownListMaNTC.Visible = true;
            lblNTC.Visible = true;
            lblMa.Text = "Mã Tiêu Chí:";
            lblMa.Visible = true;
            lblTen.Text = "Tên Tiêu Chí:";
            lblTen.Visible = true;
            txtTen.Visible = true;
            txtMa.Visible = true;
            btnLuu.Visible = true;
        }
    }
}